package com.zfxt.biz.autoconfig;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4;
import com.alibaba.fastjson.support.spring.FastJsonpHttpMessageConverter4;
import org.apache.http.client.HttpClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;

/**
 * Created by Administrator on 2018/9/11.
 */
@Configuration
public class RestAutoConfig {
        @Bean
        RestTemplate lbRestTemplate(HttpClient httpClient){
                                                                    //此方法为构建httpClient的工厂方法
            RestTemplate restemplate=new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpClient));
            //把restemplate的编码改成utf-8的模式，resttemplate接收或者发送中文的编码修改
            restemplate.getMessageConverters().add(0,new StringHttpMessageConverter(Charset.forName("UTF-8")));
            //对于接受的参数能进行json进行反序列化出来
            restemplate.getMessageConverters().add(1, new FastJsonpHttpMessageConverter5());
            return restemplate;
        }

}
